import { getCreateDate } from '@utils/pubFunction'
const dateRangeMixins = {
  data() {
    /*获取开始时间，如果早于2021-01-01，取值2021-01-01*/
    const getStartEndDate = (num) => {
      let defaultDate = '2021-01-01'
      const createTime = getCreateDate(num)
      //2021-01-01 时间戳
      let limitTime = new Date(defaultDate).getTime()
      //30天前 时间戳
      let getTime0 = new Date(createTime[0]).getTime()
      if (getTime0 < limitTime) {
        //30天前的日期，在2021-01-01日之前,开始时间设置为2021-01-01
        return [defaultDate, createTime[1]]
      } else {
        return [...createTime]
      }
    }
    return {
      /*时间选择范围：2021/01/01 至 当天*/
      pickerOptions: {
        disabledDate(time) {
          let befourTime = new Date('2021/01/01').getTime()
          return time.getTime() > Date.now() || time.getTime() < befourTime
        },
        shortcuts: [
          {
            text: '昨天',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 1)
              picker.$emit('pick', [start, start])
            }
          },
          {
            text: '过去7天',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '过去30天',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '过去90天',
            onClick(picker) {
              const end = new Date()
              /*  const start = new Date()
             start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)*/
              let [startDate] = getStartEndDate(90)
              const start = new Date(startDate)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      }
    }
  },
  methods: {
    /*获取30天日期*/
    getCreateDate(type, num = 30) {
      let defaultDate = '2021-01-01'
      const createTime = getCreateDate(num)
      //2021-01-01 时间戳
      let limitTime = new Date(defaultDate).getTime()
      //30天前 时间戳
      let getTime0 = new Date(createTime[0]).getTime()
      if (getTime0 < limitTime) {
        //30天前的日期，在2021-01-01日之前,开始时间设置为2021-01-01
        this.formInline.createTime = [defaultDate, createTime[1]]
        this.formInline['start' + type] = defaultDate
      } else {
        this.formInline.createTime = createTime
        this.formInline['start' + type] = createTime[0]
      }
      this.formInline['end' + type] = createTime[1]
    }
  }
}
export { dateRangeMixins }
